﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Nissen_Model;

namespace Nissen_Dao
{
	public class TestDao : DaoBase
	{
		public IList<Test> GetTests()
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				ICriteria crt = session.CreateCriteria<Test>();

				return crt.List<Test>();
			}

		}
		public IList<Test>GetShowTests()
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				ICriteria crt = session.CreateCriteria<Test>();

				crt.Add(Restrictions.Eq("DisplayFlag", true));

				return crt.List<Test>();
			}
		}
		public Test GetTest(string id)
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				ICriteria crt = session.CreateCriteria<Test>();

				crt.Add(Restrictions.Eq("TestId", id));

				IList<Test> tests = crt.List<Test>();

				if (tests.Count>0)
				{
					return tests[0];
				}
				return null;
			}

		}
		public void SaveOrUpdateTests(IList<Test> tests)
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				using (ITransaction t1 = session.BeginTransaction())
				{

					session.CreateSQLQuery("Delete From T_DIC_TEST").ExecuteUpdate();

					foreach (var test in tests)
					{
						session.Save(test);
					}
					t1.Commit();
				}

			}
		}


		public IList<Test> GetDisplayTests()
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				ICriteria crt = session.CreateCriteria<Test>();

				crt.Add(Restrictions.Eq("DisplayFlag", true));

				IList<Test> tests = crt.List<Test>();


				return tests;
			}
		}
	}
}
